home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Objects / moduleobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  4.8 KB  |  223 lines

  1. /* Module object implementation */
  2.  
  3. #include "Python.h"
  4.  
  5. typedef struct {
  6.     PyObject_HEAD
  7.     PyObject *md_dict;
  8. } PyModuleObject;
  9.  
  10. #include "protos/moduleobject.h"
  11.  
  12. PyObject *
  13. PyModule_New(name)
  14.     char *name;
  15. {
  16.     PyModuleObject *m;
  17.     PyObject *nameobj;
  18.     m = PyObject_NEW(PyModuleObject, &PyModule_Type);
  19.     if (m == NULL)
  20.         return NULL;
  21.     nameobj = PyString_FromString(name);
  22.     m->md_dict = PyDict_New();
  23.     if (m->md_dict == NULL || nameobj == NULL)
  24.         goto fail;
  25.     if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
  26.         goto fail;
  27.     if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
  28.         goto fail;
  29.     Py_DECREF(nameobj);
  30.     return (PyObject *)m;
  31.  
  32.  fail:
  33.     Py_XDECREF(nameobj);
  34.     Py_DECREF(m);
  35.     return NULL;
  36. }
  37.  
  38. PyObject *
  39. PyModule_GetDict(m)
  40.     PyObject *m;
  41. {
  42.     if (!PyModule_Check(m)) {
  43.         PyErr_BadInternalCall();
  44.         return NULL;
  45.     }
  46.     return ((PyModuleObject *)m) -> md_dict;
  47. }
  48.  
  49. char *
  50. PyModule_GetName(m)
  51.     PyObject *m;
  52. {
  53.     PyObject *nameobj;
  54.     if (!PyModule_Check(m)) {
  55.         PyErr_BadArgument();
  56.         return NULL;
  57.     }
  58.     nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
  59.                        "__name__");
  60.     if (nameobj == NULL || !PyString_Check(nameobj)) {
  61.         PyErr_SetString(PyExc_SystemError, "nameless module");
  62.         return NULL;
  63.     }
  64.     return PyString_AsString(nameobj);
  65. }
  66.  
  67. char *
  68. PyModule_GetFilename(m)
  69.         PyObject *m;
  70. {
  71.     PyObject *fileobj;
  72.     if (!PyModule_Check(m)) {
  73.         PyErr_BadArgument();
  74.         return NULL;
  75.     }
  76.     fileobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
  77.                        "__file__");
  78.     if (fileobj == NULL || !PyString_Check(fileobj)) {
  79.         PyErr_SetString(PyExc_SystemError, "module filename missing");
  80.         return NULL;
  81.     }
  82.     return PyString_AsString(fileobj);
  83. }
  84.  
  85. void
  86. _PyModule_Clear(m)
  87.     PyObject *m;
  88. {
  89.     /* To make the execution order of destructors for global
  90.        objects a bit more predictable, we first zap all objects
  91.        whose name starts with a single underscore, before we clear
  92.        the entire dictionary.  We zap them by replacing them with
  93.        None, rather than deleting them from the dictionary, to
  94.        avoid rehashing the dictionary (to some extent). */
  95.  
  96.     int pos;
  97.     PyObject *key, *value;
  98.     PyObject *d;
  99.  
  100.     d = ((PyModuleObject *)m)->md_dict;
  101.  
  102.     /* First, clear only names starting with a single underscore */
  103.     pos = 0;
  104.     while (PyDict_Next(d, &pos, &key, &value)) {
  105.         if (value != Py_None && PyString_Check(key)) {
  106.             char *s = PyString_AsString(key);
  107.             if (s[0] == '_' && s[1] != '_') {
  108.                 if (Py_VerboseFlag > 1)
  109.                     PySys_WriteStderr("#   clear[1] %s\n", s);
  110.                 PyDict_SetItem(d, key, Py_None);
  111.             }
  112.         }
  113.     }
  114.  
  115.     /* Next, clear all names except for __builtins__ */
  116.     pos = 0;
  117.     while (PyDict_Next(d, &pos, &key, &value)) {
  118.         if (value != Py_None && PyString_Check(key)) {
  119.             char *s = PyString_AsString(key);
  120.             if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
  121.                 if (Py_VerboseFlag > 1)
  122.                     PySys_WriteStderr("#   clear[2] %s\n", s);
  123.                 PyDict_SetItem(d, key, Py_None);
  124.             }
  125.         }
  126.     }
  127.  
  128.     /* Note: we leave __builtins__ in place, so that destructors
  129.        of non-global objects defined in this module can still use
  130.        builtins, in particularly 'None'. */
  131.  
  132. }
  133.  
  134. /* Methods */
  135.  
  136. static void
  137. module_dealloc(m)
  138.     PyModuleObject *m;
  139. {
  140.     if (m->md_dict != NULL) {
  141.         _PyModule_Clear((PyObject *)m);
  142.         Py_DECREF(m->md_dict);
  143.     }
  144.     PyObject_DEL(m);
  145. }
  146.  
  147. static PyObject *
  148. module_repr(m)
  149.     PyModuleObject *m;
  150. {
  151.     char buf[400];
  152.     char *name;
  153.     char *filename;
  154.     name = PyModule_GetName((PyObject *)m);
  155.     if (name == NULL) {
  156.         PyErr_Clear();
  157.         name = "?";
  158.     }
  159.     filename = PyModule_GetFilename((PyObject *)m);
  160.     if (filename == NULL) {
  161.         PyErr_Clear();
  162.         sprintf(buf, "<module '%.80s' (built-in)>", name);
  163.     } else {
  164.         sprintf(buf, "<module '%.80s' from '%.255s'>", name, filename);
  165.     }
  166.  
  167.     return PyString_FromString(buf);
  168. }
  169.  
  170. static PyObject *
  171. module_getattr(m, name)
  172.     PyModuleObject *m;
  173.     char *name;
  174. {
  175.     PyObject *res;
  176.     if (strcmp(name, "__dict__") == 0) {
  177.         Py_INCREF(m->md_dict);
  178.         return m->md_dict;
  179.     }
  180.     res = PyDict_GetItemString(m->md_dict, name);
  181.     if (res == NULL)
  182.         PyErr_SetString(PyExc_AttributeError, name);
  183.     else
  184.         Py_INCREF(res);
  185.     return res;
  186. }
  187.  
  188. static int
  189. module_setattr(m, name, v)
  190.     PyModuleObject *m;
  191.     char *name;
  192.     PyObject *v;
  193. {
  194.     if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
  195.         PyErr_SetString(PyExc_TypeError,
  196.                 "read-only special attribute");
  197.         return -1;
  198.     }
  199.     if (v == NULL) {
  200.         int rv = PyDict_DelItemString(m->md_dict, name);
  201.         if (rv < 0)
  202.             PyErr_SetString(PyExc_AttributeError,
  203.                    "delete non-existing module attribute");
  204.         return rv;
  205.     }
  206.     else
  207.         return PyDict_SetItemString(m->md_dict, name, v);
  208. }
  209.  
  210. PyTypeObject PyModule_Type = {
  211.     PyObject_HEAD_INIT(&PyType_Type)
  212.     0,            /*ob_size*/
  213.     "module",        /*tp_name*/
  214.     sizeof(PyModuleObject),    /*tp_size*/
  215.     0,            /*tp_itemsize*/
  216.     (destructor)module_dealloc, /*tp_dealloc*/
  217.     0,            /*tp_print*/
  218.     (getattrfunc)module_getattr, /*tp_getattr*/
  219.     (setattrfunc)module_setattr, /*tp_setattr*/
  220.     0,            /*tp_compare*/
  221.     (reprfunc)module_repr, /*tp_repr*/
  222. };
  223.